package com.example.demo.service;

import com.example.demo.domain.Girl;
import com.example.demo.enums.ResultEnum;
import com.example.demo.exception.GirlException;
import com.example.demo.repository.GirlRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by steve
 * 2018/7/10
 */

@Service
public class GirlService {

    @Autowired
    private GirlRepository repository;

    @Transactional
    public void insertTwo(){
        Girl girlA = new Girl();
        girlA.setAge(16);
        girlA.setCupSize("D");
        repository.save(girlA);

        Girl girlB = new Girl();
        girlB.setAge(20);
        girlB.setCupSize("S");
        repository.save(girlB);
    }

    public Integer getAge(Integer id) throws Exception{
        Girl girl = repository.findById(id).get();
        Integer age = girl.getAge();
        if (age < 10) {
            // code 100
            throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
        } else if (age > 10 && age < 16){
            // code 101
            throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
        } else if (age > 16){
            // code 102
            throw new GirlException(ResultEnum.COLLEGE);
        }
        return 0;
    }

    /**
     * 通过id 来查询一个女生的信息
     * @param id
     * @return
     */
    public Girl findOne(Integer id){
        return repository.findById(id).get();
    }
}
